package com.haohope.framework.accesslog;

import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.haohope.framework.FrameworkApp;
import com.haohope.framework.FrameworkConstant.SystemParam;
import com.haohope.framework.cache.ExpireCache;
import com.wb.util.LogUtil;

@SuppressWarnings("unused")
public class DefaultAccesslog extends AbstractAccesslog  implements ApplicationContextAware{
	private ExecutorService executor;
	public DefaultAccesslog() {
		executor = new ThreadPoolExecutor(1, SystemParam.accesslogPoolSize(),
                30L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>());
	}
	@Override
	public void execute(HttpServletRequest request, String logMessage) {
		executor.submit(new Runnable() {
			@Override
			public void run() {
				LogUtil.info(request, logMessage);
			}
		});
	}
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		Map<String,ExpireCache> expireCachemap = FrameworkApp.getApplicationContext().getBeansOfType(ExpireCache.class);
		if(null!=expireCachemap) {
			for(Entry<String, ExpireCache> entry:expireCachemap.entrySet()) {
				expireCache = entry.getValue();
				break;
			}
		}
	}
}
